package com.hdax.dm.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdax.dm.common.utils.CommonResponse;
import com.hdax.dm.common.utils.ResponseUtil;
import com.hdax.dm.dtos.item.IndexNavDto;
import com.hdax.dm.entity.item.DmItemType;
import com.hdax.dm.mappers.DmItemTypeMapper;
import com.hdax.dm.mapstructs.IndexNavDtoMapper;
import com.hdax.dm.service.IndexNavService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class IndexNavServiceImpl extends ServiceImpl<DmItemTypeMapper, DmItemType> implements IndexNavService {

    @Autowired
    private IndexNavDtoMapper navDtoMapper;

    @Autowired
    private RedisTemplate<String,IndexNavDto> redisTemplate;
//    @Override
//    public CommonResponse<List<IndexNavDto>> indexNavs() {
//        List<IndexNavDto> target = redisTemplate.opsForList().range("index:nav:types",0L,-1L);
//        if(target == null || target.size() == 0) {
//            List<DmItemType> sources = baseMapper.selectItemTypes();
//            target = navDtoMapper.toTargetList(sources);
//            redisTemplate.opsForList().rightPushAll("index:nav:types",target);
//            redisTemplate.expire("index:nav:types",24L, TimeUnit.HOURS);
//        }
//
//        return ResponseUtil.returnSuccess(target);
//    }

    @CachePut(cacheNames = "indexNav",keyGenerator = "ruleKeyGenerator",unless = "#result == null")
    @Override
    public CommonResponse<List<IndexNavDto>> indexNavs() {
        List<DmItemType> sources = baseMapper.selectItemTypes();
        List<IndexNavDto> target = navDtoMapper.toTargetList(sources);

        return ResponseUtil.returnSuccess(target);
    }
}
